export let teacherList = [
    {
        id: 0,
        teacherName: "teacher2",
        post: 1,
        performance: 0,
        createTime: '2024-05-16 10:19:34',
        updateTime: "2024-05-16 10:19:34"
    },
    {
        id: 1,
        teacherName: "teacher2",
        post: 1,
        performance: 0,
        createTime: '2024-05-16 10:19:34',
        updateTime: "2024-05-16 10:19:34"
    },
    {
        id: 2,
        teacherName: "teacher2",
        post: 1,
        performance: 0,
        createTime: '2024-05-16 10:19:34',
        updateTime: "2024-05-16 10:19:34"
    },
    {
        id: 3,
        teacherName: "teacher2",
        post: 1,
        performance: 0,
        createTime: '2024-05-16 10:19:34',
        updateTime: "2024-05-16 10:19:34"
    },
    {
        id: 4,
        teacherName: "teacher2",
        post: 1,
        performance: 0,
        createTime: '2024-05-16 10:19:34',
        updateTime: "2024-05-16 10:19:34"
    },
]

export default [
    //获取信息
    {
        url: '/teach',
        method: 'get',
        response: (req, res) => {
            let page = req.query.page
            let pageSize = req.query.pageSize
            let teachers: any = []
            let arr = teacherList
            for (const index in arr) {
                let key = (Number(page) - 1) * Number(pageSize) + Number(index)
                if ((Number(index) + 1) > Number(pageSize) || key  >= arr.length) {
                    break;
                }
                teachers.push(arr[key])
            }
            let teacher = {
                total: teacherList.length,
                rows: teachers
            }
            return { code: 200, messages: '获取教师信息成功', data: { teacher } }
        }
    },
    //增加信息
    {
        url: '/teach',
        method: 'post',
        response: ({ body }) => {
            
            let { teacherName, post } = body;
            let nowTime = Date.now()
            let data = new Date(nowTime).toLocaleString().replace(/\//g, "-")
            let object: any = {}
            let length = teacherList.length
            object.id = length
            object.createTime = data
            object.post = post
            object.teacherName = teacherName
            object.performance = 0
            teacherList.push(object)
            if (length < teacherList.length) {
                return { code: 200, messages: "添加教师成功", data: null }
            } else {
                return { code: 400, messages: '数据异常' }
            }

        }
    },
    {
        url: '/teach',
        method: 'delete',
        response: (req,res) => {
            console.log(req)
            let id  = req.query.id
            let list: any = teacherList.findIndex(el => el.id === Number(id));
            if (list !== -1) {
                // 从教师列表中删除教师数据
                teacherList.splice(list, 1);
                return { code: 200, messages: "删除教师成功", data: null }
            } else {
                return {
                    code: 400,
                    messages: "数据异常"
                }
            }
        }

    },
    {
        url: '/teach',
        method: 'patch',
        response: ({ body }) => {
            const { teacherName, performance, id } = body;
            // 查找要修改的教师数据的索引
            const index: any = teacherList.findIndex(teacher => teacher.id === Number(id));
            if (index !== -1) {
                // 更新教师数据
                teacherList[index].teacherName = teacherName || teacherList[index].teacherName;
                teacherList[index].performance = performance || teacherList[index].performance;
                return { code: 200, messages: '修改教师成功' } // 返回修改后的教师数据
            } else {
                return { code: 200, messages: "数据异常" } // 未找到教师数据，返回400状态码
            }
        }

    },
    {
        //搜索
        url: '/teach/pagingQueries',
        method: 'get',
        response: (req,res) => {
            console.log(req)
            let id  = req.query.id
            let list: any = teacherList.findIndex(el => el.id === Number(id));
            if (list !== -1) {
                return { code: 200, messages: "获取教师信息成功", data:{ teacher:teacherList[list]} }
            } else {
                return {
                    code: 400,
                    messages: "获取失败"
                }
            }
        }

    },
]